package a10_动态规划;

/**
 * <p>
 * a53_最长回文子序列
 * </p>
 *
 * @author flyduck
 * @since 2025/3/5
 */
public class a53_最长回文子序列 {
    public int longestPalindromeSubseq(String s) {
        char[] chars = s.toCharArray();

        int[][] dp = new int[chars.length][chars.length];
        for (int i = 0; i < chars.length; i++) {
            dp[i][i] = 1;
        }

        for (int i = chars.length - 1; i >= 0; i--) {
            for (int j = i+1; j < chars.length; j++) {
                if(chars[i] == chars[j]){
                    dp[i][j] = dp[i+1][j-1] + 2;
                }else {
                    dp[i][j] = Math.max(dp[i+1][j],dp[i][j-1]);
                }
            }
        }
        return dp[0][chars.length-1];
    }
}
